/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.autosalon.ui.customer;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.ui.customer.action.SaveUpdateCustomerAction;
import com.autosalon.model.Zakaznik;
import com.autosalon.ui.ContentSwitcher;
import com.autosalon.ui.customer.action.AddAdressAction;
import com.autosalon.ui.customer.action.AddCustomerAction;
import com.autosalon.ui.customer.action.BackEditCustomerAction;
import com.autosalon.ui.customer.action.DeleteCustomerAction;
import com.autosalon.ui.customer.action.EditCustomerAction;
import com.autosalon.ui.customer.action.RemoveAdressAction;
import com.autosalon.ui.customer.action.SaveNewCustomerAction;
import com.autosalon.ui.customer.state.CustomerStateHolder;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Kubík
 */
public class Customer extends HorizontalLayout {

    private final ContentSwitcher switcher;
    private Label lbAlreadyRegister;
    private Label lbSaveUpdateCustomer;
    private Label lbSaveCustomer;
    private Label lbAdress;
    private VerticalLayout layoutAddCustomer;
    private TextField texFirstName;
    private TextField texLastName;
    private PasswordField texPassword;
    private PasswordField texControlPassword;
    private TextField texMobil;
    private TextField texEmail;
    private TextField filterFieldName;
    private TextField filterFieldSname;
    private TextField texCO;
    private TextField texCP;
    private TextField texMesto;
    private TextField texPSC;
    private TextField texStat;
    private TextField texUlice;
    private TextArea texNote;
    private Button btnAddCustumer;
    private Button btnSaveEdistCustomer;
    private Button btnEditCustomer;
    private Button btnEditBackCustomer;
    private Button btnNewCustomer;
    private Button btnAddAdress;
    private Button btnRemoveAdress;
    private Zakaznik zakaznik;
    private Form newCustomer;
    private Form adress;
    private List<Form> newAdress = new ArrayList<Form>();
    private Table customers;
    private ComboBox comRole;
    private int countOfAdress = 1;

    public Customer(ContentSwitcher switcher) {
        this.switcher = switcher;
        init();
    }

    public void init() {
        btnRemoveAdress = new Button("Odebraní adresy");
        btnRemoveAdress.addListener(new RemoveAdressAction(this));
        btnNewCustomer = new Button("Nový zákazník");
        btnNewCustomer.addListener(new AddCustomerAction(this));
        btnAddAdress = new Button("Přidání adresy");
        btnAddAdress.addListener(new AddAdressAction(this));
        btnEditCustomer = new Button("Změna osobních údajů");
        btnEditCustomer.addListener(new EditCustomerAction(this));
        if (getSwitcher().getMainWindow().getZakaznik() != null) {
            btnEditCustomer.setData(getSwitcher().getMainWindow().getZakaznik().getId());
        }
        btnEditBackCustomer = new Button("Zpět");
        btnEditBackCustomer.addListener(new BackEditCustomerAction(this));
        btnSaveEdistCustomer = new Button("Ulož změny");
        btnSaveEdistCustomer.addListener(new SaveUpdateCustomerAction(this, zakaznik));
        btnAddCustumer = new Button("Vlož");
        btnAddCustumer.addListener(new SaveNewCustomerAction(this));

        texFirstName = new TextField("Jméno");
        texFirstName.setRequired(true);
        texLastName = new TextField("Příjmení");
        texLastName.setRequired(true);
        texPassword = new PasswordField("Heslo");
        texPassword.setRequired(true);
        texControlPassword = new PasswordField("Heslo znova");
        texControlPassword.setRequired(true);
        texMobil = new TextField("Mobil");
        texMobil.setRequired(true);
        texEmail = new TextField("E-mail");
        texEmail.setRequired(true);
        texEmail.setValue("@");
        texNote = new TextArea("    Poznámka");
        texNote.setHeight("100px");
        texNote.setWidth("200px");

        lbAlreadyRegister = new Label("<font size=3  color=red>Již jste registrován v databázi autosalonu!</font>");
        lbAlreadyRegister.setContentMode(Label.CONTENT_XHTML);
        lbSaveUpdateCustomer = new Label("Změna údajů proběhla úspěšně.");
        lbSaveCustomer = new Label("Registrace proběhla úspěšně.");



        final IndexedContainer ic = new IndexedContainer();
        ic.addContainerProperty("Křestní jméno", String.class, null);
        ic.addContainerProperty("Přijmení", String.class, null);
        ic.addContainerProperty("Mobil", String.class, null);
        ic.addContainerProperty("E-mail", String.class, null);
        ic.addContainerProperty("Password", String.class, null);
        ic.addContainerProperty("Role", String.class, null);
        ic.addContainerProperty("Edit", Button.class, null);
        ic.addContainerProperty("Delete", Button.class, null);


        filterFieldName = new TextField("Vyhledávání podle jména ");
        filterFieldName.setTextChangeEventMode(TextChangeEventMode.LAZY);
        filterFieldName.setTextChangeTimeout(200);
        filterFieldName.addListener(new TextChangeListener() {

            @Override
            public void textChange(TextChangeEvent event) {
                ic.removeAllContainerFilters();
                ic.addContainerFilter("Křestní jméno", event.getText(), true, false);
            }
        });
        filterFieldSname = new TextField("Vyhledávání podle příjmení ");
        filterFieldSname.setTextChangeEventMode(TextChangeEventMode.LAZY);
        filterFieldSname.setTextChangeTimeout(200);
        filterFieldSname.addListener(new TextChangeListener() {

            @Override
            public void textChange(TextChangeEvent event) {
                ic.removeAllContainerFilters();
                ic.addContainerFilter("Přijmení", event.getText(), true, false);
            }
        });

        customers = new Table("Zákazníci");
        customers.setSizeFull();
        customers.setSelectable(true);
        customers.setWidth("100%");
        customers.setHeight("100%");
        customers.setContainerDataSource(ic);
        customers.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                event.getProperty().getValue();
            }
        });

        fillTable();

        comRole = new ComboBox("Zvolte roli");
        comRole.addItem("Customer");
        comRole.addItem("Seller");
        comRole.setFilteringMode(Filtering.FILTERINGMODE_OFF);
        comRole.setImmediate(true);
        comRole.select("Customer");

        texStat = new TextField("Stát");
        texMesto = new TextField("Město");
        texUlice = new TextField("Ulice");
        texCP = new TextField("Č. popisné");
        texCO = new TextField("Č. orientační");
        texPSC = new TextField("PSČ");
        lbAdress = new Label("Adresa");

        if (adress == null) {
            adress = new Form();
            adress.getLayout().addComponent(lbAdress);
            adress.getLayout().addComponent(texStat);
            adress.getLayout().addComponent(texMesto);
            adress.getLayout().addComponent(texUlice);
            adress.getLayout().addComponent(texCP);
            adress.getLayout().addComponent(texCO);
            adress.getLayout().addComponent(texPSC);
            newAdress.add(adress);
        }

        newCustomer = new Form();
        newCustomer.setCaption("Registrace zákazníka");
        newCustomer.getLayout().addComponent(texFirstName);
        newCustomer.getLayout().addComponent(texLastName);
        newCustomer.getLayout().addComponent(comRole);
        newCustomer.getLayout().addComponent(texPassword);
        newCustomer.getLayout().addComponent(texControlPassword);
        newCustomer.getLayout().addComponent(texMobil);
        newCustomer.getLayout().addComponent(texEmail);
        newCustomer.getLayout().addComponent(texNote);


        setSpacing(true);








        addComponent();
        int id;
        try {
            id = getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue();
        } catch (NullPointerException e) {
            id = 1;
        }
        CustomerStateHolder.setState(CustomerStateHolder.NEW_CUSTOMER_STATE, id, this);
    }

    public void addComponent() {
        removeAllComponents();
        layoutAddCustomer = new VerticalLayout();
        layoutAddCustomer.setSpacing(true);

        layoutAddCustomer.addComponent(filterFieldName);
        layoutAddCustomer.addComponent(filterFieldSname);
        layoutAddCustomer.addComponent(newCustomer);
        for (Iterator<Form> it = newAdress.iterator(); it.hasNext();) {
            Form form = it.next();
            layoutAddCustomer.addComponent(form);
        }
        layoutAddCustomer.addComponent(customers);
        layoutAddCustomer.addComponent(lbSaveCustomer);
        layoutAddCustomer.addComponent(btnAddAdress);
        layoutAddCustomer.addComponent(btnRemoveAdress);
        layoutAddCustomer.addComponent(lbSaveUpdateCustomer);
        layoutAddCustomer.addComponent(btnAddCustumer);
        layoutAddCustomer.addComponent(lbAlreadyRegister);
        layoutAddCustomer.addComponent(btnEditCustomer);
        layoutAddCustomer.addComponent(btnSaveEdistCustomer);
        layoutAddCustomer.addComponent(btnEditBackCustomer);
        layoutAddCustomer.addComponent(btnNewCustomer);
        layoutAddCustomer.setSizeFull();

        setSpacing(true);
        addComponent(layoutAddCustomer);
    }

    public void fillTable() {
        List<Object> actualCustomers = new ArrayList<Object>();
        actualCustomers = DaoFactory.getinstance().getZakaznik().getAll(0, Integer.MAX_VALUE);
        customers.removeAllItems();
        for (Object ob : actualCustomers) {
            Zakaznik cust = (Zakaznik) ob;
            Button editCustomer = new Button("Edit");
            Button deleteCustomer = new Button("Delete");
            editCustomer.setStyleName(Button.STYLE_LINK);
            editCustomer.addListener(new EditCustomerAction(this));
            deleteCustomer.setStyleName(Button.STYLE_LINK);
            deleteCustomer.addListener(new DeleteCustomerAction(this));
            deleteCustomer.setEnabled(false);
            editCustomer.setData(cust.getId());
            deleteCustomer.setData(cust.getId());
            customers.addItem(new Object[]{cust.getMeno().toString(), cust.getPrimeni().toString(), cust.getMobil().toString(), cust.getEmail().toString(), cust.getPassword().toString(), cust.getRole().getName(), editCustomer, deleteCustomer}, null);
        }
    }

    public Button getBtnAddCustumer() {
        return btnAddCustumer;
    }

    public void setBtnAddCustumer(Button btnAddCustumer) {
        this.btnAddCustumer = btnAddCustumer;
    }

    public VerticalLayout getLayoutAddCustomer() {
        return layoutAddCustomer;
    }

    public void setLayoutAddCustomer(VerticalLayout layoutAddCustomer) {
        this.layoutAddCustomer = layoutAddCustomer;
    }

    public TextField getTexEmail() {
        return texEmail;
    }

    public void setTexEmail(TextField texEmail) {
        this.texEmail = texEmail;
    }

    public TextField getTexFirstName() {
        return texFirstName;
    }

    public void setTexFirstName(TextField texFirstName) {
        this.texFirstName = texFirstName;
    }

    public TextField getTexLastName() {
        return texLastName;
    }

    public void setTexLastName(TextField texLastName) {
        this.texLastName = texLastName;
    }

    public TextField getTexMobil() {
        return texMobil;
    }

    public void setTexMobil(TextField texMobil) {
        this.texMobil = texMobil;
    }

    public TextArea getTexNote() {
        return texNote;
    }

    public void setTexNote(TextArea texNote) {
        this.texNote = texNote;
    }

    public ContentSwitcher getSwitcher() {
        return switcher;
    }

    public PasswordField getTexControlPassword() {
        return texControlPassword;
    }

    public void setTexControlPassword(PasswordField texControlPassword) {
        this.texControlPassword = texControlPassword;
    }

    public PasswordField getTexPassword() {
        return texPassword;
    }

    public void setTexPassword(PasswordField texPassword) {
        this.texPassword = texPassword;
    }

    public Button getBtnEditCustomer() {
        return btnEditCustomer;
    }

    public void setBtnEditCustomer(Button btnEditCustomer) {
        this.btnEditCustomer = btnEditCustomer;
    }

    public Button getBtnSaveEdistCustomer() {
        return btnSaveEdistCustomer;
    }

    public void setBtnSaveEdistCustomer(Button btnSaveEdistCustomer) {
        this.btnSaveEdistCustomer = btnSaveEdistCustomer;
    }

    public Label getLbAlreadyRegister() {
        return lbAlreadyRegister;
    }

    public void setLbAlreadyRegister(Label lbAlreadyRegister) {
        this.lbAlreadyRegister = lbAlreadyRegister;
    }

    public Label getLbSaveCustomer() {
        return lbSaveCustomer;
    }

    public void setLbSaveCustomer(Label lbSaveCustomer) {
        this.lbSaveCustomer = lbSaveCustomer;
    }

    public Label getLbSaveUpdateCustomer() {
        return lbSaveUpdateCustomer;
    }

    public void setLbSaveUpdateCustomer(Label lbSaveUpdateCustomer) {
        this.lbSaveUpdateCustomer = lbSaveUpdateCustomer;
    }

    public Form getNewCustomer() {
        return newCustomer;
    }

    public void setNewCustomer(Form newCustomer) {
        this.newCustomer = newCustomer;
    }

    public Table getCustomers() {
        return customers;
    }

    public void setCustomers(Table customers) {
        this.customers = customers;
    }

    public Button getBtnEditBackCustomer() {
        return btnEditBackCustomer;
    }

    public void setBtnEditBackCustomer(Button btnEditBackCustomer) {
        this.btnEditBackCustomer = btnEditBackCustomer;
    }

    public Button getBtnNewCustomer() {
        return btnNewCustomer;
    }

    public void setBtnNewCustomer(Button btnNewCustomer) {
        this.btnNewCustomer = btnNewCustomer;
    }

    public TextField getFilterFieldName() {
        return filterFieldName;
    }

    public void setFilterFieldName(TextField filterFieldName) {
        this.filterFieldName = filterFieldName;
    }

    public TextField getFilterFieldSname() {
        return filterFieldSname;
    }

    public void setFilterFieldSname(TextField filterFieldSname) {
        this.filterFieldSname = filterFieldSname;
    }

    public ComboBox getComRole() {
        return comRole;
    }

    public void setComRole(ComboBox comRole) {
        this.comRole = comRole;
    }

    public Label getLbAdress() {
        return lbAdress;
    }

    public void setLbAdress(Label lbAdress) {
        this.lbAdress = lbAdress;
    }

    public TextField getTexCO() {
        return texCO;
    }

    public void setTexCO(TextField texCO) {
        this.texCO = texCO;
    }

    public TextField getTexCP() {
        return texCP;
    }

    public void setTexCP(TextField texCP) {
        this.texCP = texCP;
    }

    public TextField getTexMesto() {
        return texMesto;
    }

    public void setTexMesto(TextField texMesto) {
        this.texMesto = texMesto;
    }

    public TextField getTexPSC() {
        return texPSC;
    }

    public void setTexPSC(TextField texPSC) {
        this.texPSC = texPSC;
    }

    public TextField getTexStat() {
        return texStat;
    }

    public void setTexStat(TextField texStat) {
        this.texStat = texStat;
    }

    public TextField getTexUlice() {
        return texUlice;
    }

    public void setTexUlice(TextField texUlice) {
        this.texUlice = texUlice;
    }

    public Zakaznik getZakaznik() {
        return zakaznik;
    }

    public void setZakaznik(Zakaznik zakaznik) {
        this.zakaznik = zakaznik;
    }

    public Button getBtnAddAdress() {
        return btnAddAdress;
    }

    public void setBtnAddAdress(Button btnAddAdress) {
        this.btnAddAdress = btnAddAdress;
    }

    public int getCountOfAdress() {
        return countOfAdress;
    }

    public void setCountOfAdress(int countOfAdress) {
        this.countOfAdress = countOfAdress;
    }

    public List<Form> getNewAdress() {
        return newAdress;
    }

    public void setNewAdress(List<Form> newAdress) {
        this.newAdress = newAdress;
    }

    public Button getBtnRemoveAdress() {
        return btnRemoveAdress;
    }

    public void setBtnRemoveAdress(Button btnRemoveAdress) {
        this.btnRemoveAdress = btnRemoveAdress;
    }
}
